﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
/*说明：
 首先要在UI放外围uiPanel1的位置（150, 120），以及放内围panel1的位置（100,100）,再放个容器panel2包含uiPanel1和panel1
一条边的Pin个数：SideNumber; 
外围边长：uiPanel1.Width = OutSideLength = (40 * SideNumber - 15) + (40 * 2);  = 385
内围边长：panel1.Width = InSideLength = uipanel1.Width - 100 * 2；= 185
内围上侧和右侧虚线边长：SideLength = panel1.Width - 10；内围左侧和下侧虚线边长：SideLength - 50；
pin的方格大小和位置：大小：InTextBox.Size = new Size(25, 25);
                     位置：1、if (i >= 1 && i <= SideNumber) //下侧
                                {InTextBox.Location = new Point(40 + (i - 1) * 40, OutSideLength - 25 - 1);}
                           2、if (i > SideNumber && i <= SideNumber * 2) //右侧
                                {InTextBox.Location = new Point(OutSideLength - 25 - 1, OutSideLength - 25 - (i - SideNumber) * 40);}
                           3、if (i > SideNumber * 2 && i <= SideNumber * 3) //上侧
                                {InTextBox.Location = new Point(OutSideLength - 25 - (i - SideNumber * 2) * 40, 1);}
                           4、if (i > SideNumber * 3 && i <= SideNumber * 4) //左侧
                                {InTextBox.Location = new Point(1, 40 + (i - 1 - SideNumber * 3) * 40);}
pin的Label位置、大小和名称：
        上下侧大小：(25, 110)；下侧位置：OutLabel.Left = uiPanel1.Left + InTextBox.Left; OutLabel.Top = uiPanel1.Top + uiPanel1.Height + 20;
                              上侧位置：OutLabel.Left = uiPanel1.Left + InTextBox.Left; OutLabel.Top = 10;
        左右侧大小：(110, 25)；右侧位置：OutLabel.Left = uiPanel1.Left + OutSideLength + 3; OutLabel.Top = uiPanel1.Top + InTextBox.Top;
                              左侧位置：OutLabel.Left = uiPanel1.Left - OutLabel.Width - 3; OutLabel.Top = uiPanel1.Top + InTextBox.Top;
*/

namespace NovoSerDes
{
    public partial class PinMapSerSetup : UIPage
    {
        private List<string> iniFilePaths = new List<string>();
        private List<string> sections = new List<string>();
        private List<List<string>> keys = new List<List<string>>();
        List<UILabel> Labels = new List<UILabel> { };
        List<UIContextMenuStrip> ContextMenuStrips = new List<UIContextMenuStrip> { };
        int pinIndex = 1;
        int curpinIndex = 1;

        //[DllImport("kernel32")]
        //private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        private int SideNumber; //一条边的Pin个数
        private int OutSideLength;
        private int InSideLength;
        //字段初始值设定项不能引用非静态字段、方法或属性“name”。要在构造函数中初始化
        public PinMapSerSetup()
        {
            InitializeComponent();
            SideNumber = 8; //设置一条边的Pin个数

            OutSideLength = (40 * SideNumber - 15) + (40 * 2);
            uiPanel1.Width = OutSideLength;
            uiPanel1.Height = OutSideLength;
            InSideLength = OutSideLength - 100 * 2;
            panel1.Width = InSideLength;
            panel1.Height = InSideLength;
            InitStart(); //画Pin的方格和Label
            uiLabel_9116.Left = InSideLength / 2 - uiLabel_9116.Width / 2;
            uiLabel_9116.Top = InSideLength / 2 - uiLabel_9116.Height / 2;
            uiAvatar1.Left = 3;
            uiAvatar1.Top = InSideLength - 3 - uiAvatar1.Height;
            uiLabel_EP.Left = InSideLength - 10 - uiLabel_EP.Width;
            uiLabel_EP.Top = InSideLength - 10 - uiLabel_EP.Height;
            DpiSetSerPin();
        }

        private void CenterRectPaint(PaintEventArgs e)
        {
            Pen p = new Pen(Color.Black, 1);
            int SideLength = this.panel1.Width - 10;
            p.DashPattern = new float[] { 4, 2 };//自定义虚线,设置短划线和空白部分的数组
            e.Graphics.DrawLine(p, 0, 0, SideLength, 0);
            e.Graphics.DrawLine(p, SideLength, 0, SideLength, SideLength);
            e.Graphics.DrawLine(p, 0, 0, 0, SideLength - 50);
            e.Graphics.DrawLine(p, 50, SideLength, SideLength, SideLength);
            e.Graphics.DrawLine(p, 0, SideLength - 50, 50, SideLength); //绘制斜线
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            CenterRectPaint(e);
        }

        private void InitStart()
        {
            List<UITextBox> textBoxes = new List<UITextBox> { };

            for (int i = 1; i <= SideNumber * 4; i++)
            {
                UITextBox InTextBox = new UITextBox();
                InTextBox.Text = i.ToString("D");
                InTextBox.Name = "uiTextBox" + i.ToString("D");
                InTextBox.FillColor = this.uiPanel1.FillColor;
                InTextBox.Size = new Size(25, 25);

                UILabel OutLabel = new UILabel();
                OutLabel.Text = i.ToString("D");
                OutLabel.Name = "uiLabel" + i.ToString("D");
                OutLabel.Cursor = Cursors.Hand;
                if (i >= 1 && i <= SideNumber || i > SideNumber * 2 && i <= SideNumber * 3)
                {
                    OutLabel.Size = new Size(25, 110);
                    OutLabel.Angle = 270;
                    OutLabel.TextAlign = ContentAlignment.MiddleCenter;
                }
                if (i > SideNumber && i <= SideNumber * 2 || i > SideNumber * 3 && i <= SideNumber * 4)
                {
                    OutLabel.Size = new Size(110, 25);
                }

                if (i >= 1 && i <= SideNumber)
                {
                    InTextBox.Location = new Point(40 + (i - 1) * 40, OutSideLength - 25 - 1);
                    OutLabel.Left = uiPanel1.Left + InTextBox.Left;
                    OutLabel.Top = uiPanel1.Top + uiPanel1.Height + 20;
                }
                if (i > SideNumber && i <= SideNumber * 2)
                {
                    InTextBox.Location = new Point(OutSideLength - 25 - 1, OutSideLength - 25 - (i - SideNumber) * 40);
                    OutLabel.Left = uiPanel1.Left + OutSideLength + 3;
                    OutLabel.Top = uiPanel1.Top + InTextBox.Top;
                }
                if (i > SideNumber * 2 && i <= SideNumber * 3)
                {
                    InTextBox.Location = new Point(OutSideLength - 25 - (i - SideNumber * 2) * 40, 1);
                    OutLabel.Left = uiPanel1.Left + InTextBox.Left;
                    OutLabel.Top = 10;
                }
                if (i > SideNumber * 3 && i <= SideNumber * 4)
                {
                    InTextBox.Location = new Point(1, 40 + (i - 1 - SideNumber * 3) * 40);
                    OutLabel.Left = uiPanel1.Left - OutLabel.Width - 3;
                    OutLabel.Top = uiPanel1.Top + InTextBox.Top;
                }
                uiPanel1.Controls.Add(InTextBox);
                textBoxes.Add(InTextBox);

                panel2.Controls.Add(OutLabel);
                Labels.Add(OutLabel);
            }
            foreach (var textBox in textBoxes)
            {
                textBox.RectSides = ToolStripStatusLabelBorderSides.None;
                textBox.Paint += new System.Windows.Forms.PaintEventHandler(this.uiTextBox_Paint);
            }
        }

        private void uiTextBox_Paint(object sender, PaintEventArgs e)
        {
            Pen p = new Pen(Color.Black, 1);
            p.DashPattern = new float[] { 4, 2 };
            e.Graphics.DrawRectangle(p, 0, 0, 24, 24);
        }

        private void PinMapSerSetup1_Load(object sender, EventArgs e)
        {
            string path = System.IO.Path.Combine(Environment.CurrentDirectory, "PinMap INI FILE");
            if (!Directory.Exists(path))
            {
                UIMessageDialog.ShowErrorDialog(FindForm(), "找不到ini文件夹");
            }
            else
            {
                string[] iniFiles = Directory.GetFiles(path, "*.ini");
                iniFilePaths.Clear();
                iniFilePaths.AddRange(iniFiles);   //所有的ini文件路径集合
                if (iniFilePaths.Count == 0)
                {
                    UIMessageDialog.ShowErrorDialog(FindForm(), "找不到ini文件");
                }
                else
                {
                    bool isFoundIniFile = false; 
                    foreach (string iniFilePath in iniFilePaths)
                    {
                        IniFiles.IniFile iniFile = new IniFiles.IniFile(iniFilePath);
                        if (iniFile.ReadString("Name", "name", null) == "NLS9116")
                        {
                            //UIMessageDialog.ShowSuccessDialog(FindForm(), "找到NLS9116的PinMap ini文件");
                            isFoundIniFile = true;
                            sections.Clear();
                            sections.AddRange(iniFile.ReadSections);   //ini文件所有的section集合
                            keys.Clear();
                            foreach (string section in sections)
                            {
                                if (section == "Name") continue;
                                Labels[pinIndex - 1].Text = section;
                                Labels[pinIndex - 1].Tag = pinIndex; // 设置Tag属性为当前索引
                                Labels[pinIndex - 1].Click += new System.EventHandler(this.uiLabel_Click);
                                UIContextMenuStrip OutMenuStrip = new UIContextMenuStrip();
                                OutMenuStrip.Name = "uiContextMenuStrip" + pinIndex.ToString("D");
                                ContextMenuStrips.Add(OutMenuStrip);
                                OutMenuStrip.ItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.uiContextMenuStrip_ItemClicked);

                                List<string> keylist = new List<string>(iniFile.ReadKeyNames(section));
                                if (section == "*MFP0")
                                {
                                    foreach (string key in keylist)
                                    {
                                        System.Console.WriteLine(key);
                                    }

                                }
                                foreach (string key in keylist)
                                {
                                    ToolStripMenuItem toolStripMenuItem1 = new ToolStripMenuItem(key);
                                    toolStripMenuItem1.Text = key;
                                    OutMenuStrip.Items.Add(toolStripMenuItem1);
                                }
                                keys.Add(keylist);

                                pinIndex++;
                            }
                        }
                    }
                    if (!isFoundIniFile)
                    {
                        UIMessageDialog.ShowErrorDialog(FindForm(), "没有找到NLS9116的PinMap ini文件");
                    }
                }
            }
        }

        private void uiLabel_Click(object sender, EventArgs e) //object sender是触发事件的对象(控件)的引用
        {
            // 将sender对象转换为Label类型，以访问其属性  
            UILabel clickedLabel = sender as UILabel;
            int labelIndex = (int)clickedLabel.Tag;
            curpinIndex = labelIndex;
            switch (labelIndex)
            {
                case 100:
                    System.Console.WriteLine("hello1");
                    break;
                case 101:
                    System.Console.WriteLine("hello2");
                    break;
                default:
                    System.Console.WriteLine("hello" + labelIndex.ToString());
                    System.Console.WriteLine(ContextMenuStrips[labelIndex - 1].Name);
                    System.Console.WriteLine("curpinIndex:" + curpinIndex.ToString());
                    clickedLabel.ShowContextMenuStrip(ContextMenuStrips[labelIndex - 1], 0, clickedLabel.Height);
                    break;
            }
        }

        private void uiContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            // 将sender对象转换为UIContextMenuStrip类型，以访问其属性  
            UIContextMenuStrip clickedOutMenuStrip = sender as UIContextMenuStrip;
            if (e.ClickedItem != null)
            {
                System.Console.WriteLine("curpinIndex1:" + curpinIndex.ToString());
                Labels[curpinIndex - 1].Text = "*" + e.ClickedItem.Text;
                switch (e.ClickedItem.Text)
                {
                    case "GPIO0":
                        System.Console.WriteLine("GPIO0 config");
                        break;
                    case "test_bus[0]":
                        System.Console.WriteLine("test_bus[0] config");
                        break;
                    case "ERRB":
                        System.Console.WriteLine("ERRB config");
                        break;
                    case "EXT100M Clock":
                        System.Console.WriteLine("EXT100M Clock config");
                        break;
                    case "ADC0":
                        System.Console.WriteLine("ADC0 config");
                        break;
                    default:
                        System.Console.WriteLine("else config");
                        break;
                }
            }
        }

        private float GetDPI()
        {
            using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
            {
                float dpiX = graphics.DpiX;
                float dpiY = graphics.DpiY;
                return dpiX;
            }
        }

        private void DpiSetSerPin()
        {
            double dpi = GetDPI();
            double scale = dpi / 0.96;
            System.Console.WriteLine(scale);
            if (scale == 150 || scale == 125) //100% - 96 DPI;125% - 120 DPI;150% - 144 DPI;175% - 168 DPI;200% - 192 DPI
            {
                foreach (Control control in panel2.Controls)
                {
                    // 检查控件是否有Font属性，并且是否支持更改  
                    if (control.Font != null)
                    {
                        control.Font = new System.Drawing.Font("宋体", 8F);
                    }
                }

                foreach (Control control in uiPanel1.Controls)
                {
                    // 检查控件是否有Font属性，并且是否支持更改  
                    if (control.Font != null)
                    {
                        control.Font = new System.Drawing.Font("宋体", 8F);
                    }
                }

                this.uiLabel_9116.Font = new System.Drawing.Font("宋体", 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                this.uiLabel_EP.Font = new System.Drawing.Font("宋体", 8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            }
        }
    }
}
